using System;
using System.Reflection;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;

using System.Security;
using System.Security.Permissions;
using System.Web;
using System.Web.Security;
using Microsoft.SharePoint.Security;


namespace stsdev {

  public class WebPartWithCasPermissionsSolutionProvider : IPartiallyTrustedSolutionProvider {


    public string Title {
      get { return "Web Part Solution with CAS Permission (C# Assembly)"; }
    }

    //Added 2/10/08 DMANN
    public bool LaunchSolutionOnComplete
    {
        get { return false; }
    }


    public void InitializeSolution() {
      SolutionBuilder.AssemblyDeployment = true;
      SolutionBuilder.SafeControlSettings = true;
      SolutionBuilder.DeploymentTarget = AssemblyDeploymentTarget.WebApplication;
      SolutionBuilder.CasPermissions = true;
    }

    public void AddSolutionItems() {
      WebPartBuilder.CreateWebPart("PartialTrustWebPart1");
      WebPartBuilder.CreateWebPart("PartialTrustWebPart2");
      WebPartBuilder.CreateWebPartFeature();
    }

    public PermissionSet CreatePermissionSet() {

      PermissionSet ps = new NamedPermissionSet(SolutionBuilder.TargetName, PermissionState.None);
      
      // (1) Add security perm
      SecurityPermission sp = 
        new SecurityPermission(SecurityPermissionFlag.Execution |
                               SecurityPermissionFlag.SerializationFormatter |
                               SecurityPermissionFlag.ControlPrincipal);
      ps.AddPermission(sp);
      
      // (2) Add file perm
      FileIOPermission fp = new FileIOPermission(PermissionState.None);
      fp.AllFiles = FileIOPermissionAccess.Read;
      ps.AddPermission(fp);

      // (3) Add ASP Hosting perm      
      ps.AddPermission(new AspNetHostingPermission(AspNetHostingPermissionLevel.Unrestricted));

      // (4) Add SharePoint perm
      SharePointPermission spp = new SharePointPermission(PermissionState.None);
      spp.Impersonate = true;
      spp.ObjectModel = true;
      spp.UnsafeSaveOnGet = true;
      ps.AddPermission(spp);

      // return PermissionSet
      return ps;
    }
  }
}
